//
//  JWKMainViewController.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/19.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit

class JWKMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //利用kvc来进行修改tabbar
//        CGRectMake(0, 0, , 49)
        let tabbar:JWKTabBar = JWKTabBar(frame: CGRectMake(0, 0, UIScreenWidth, 49))
        
        //显示tabBar的类名
        print(tabBar.classForCoder)
        
        //利用kvc来进行修改tabbar
        setValue(tabbar, forKey: "tabBar")
        
        print(tabBar.classForCoder)
        
        addChildViewControllers()
        
    }

  
   private func addChildViewControllers() {
        
        //创建和添加4个页面的viewController
        addChildViewController(JWKHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(JWKMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(JWKDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(JWKProfileTableViewController(), imageName: "tabbar_profile", title: "我的")
        
        
    }
    
    private func addChildViewController(vc:UIViewController, imageName:String, title:String) {
        
//        var vc : UIViewController = JWKHomeTableViewController()
        //        vc.navigationItem.title = "首页"
        //        vc.tabBarItem.title = "首页"
        
        //把系统的barItem替换成自定义的barItem,这样的话点击item显示提醒数字和背景图的时候也会自动调用我们修改背景图的方法,从而保证每次都是我们想要的效果---创建自定义tabBarItem模型
        
        vc.tabBarItem = JWKTabBarItem()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //绑定tag值,方便后面判断查找使用
        vc.tabBarItem.tag = childViewControllers.count
//        print(vc.tabBarItem.tag)
        
        //设置标签的图片的颜色--渲染方式
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //.imageWithRenderingMode(.AlwaysOriginal) 按照图片的真实颜色来渲染图片
        
        
        
        //统一设置tabbar里标签内容的颜色,包括图片和文字
        //        tabBar.tintColor = UIColor.orangeColor()
        
        //设置标签的文字颜色 NSForegroundColorAttributeName前景文字颜色 Selected选中状态
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        //设置标签文字的偏移y方向
        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3)
        let nav = JWKBaseNavController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    //重写协议方法
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
//        UITabBarSwappableImageView
//        print(item.title)
//        print(item.tag)
        
        var index = 0
        for subview in tabBar.subviews{
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
//                print(subview.subviews)
                if index == item.tag{
                    for item in subview.subviews{
                        if item.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            //找到了tabBar中BarButton中的_UITabBarBackgroundView
                            item.transform = CGAffineTransformMakeScale(1.2, 1.2)
                            UIView.animateWithDuration(0.25, animations: { 
                                item.transform = CGAffineTransformIdentity
                            })
                        }
                    }
                }
                
                index = index + 1
                
            }
            
        }
    }
    
    //TODO: 明天继续
    //FIXME: 安徽
}
